
public class Main {
    static int totalFruit(int[] fruits) {
        int left = 0,right = 0,n = fruits.length;
        int[] hash = new int[n+1];
        int len = 0,kinds = 0;
        while (left < n && right < n) {

            if (hash[fruits[right]] == 0) {
                kinds++;
            }
            hash[fruits[right]]++;
            while (kinds > 2) {
                hash[fruits[left]]--;
                if (hash[fruits[left]] == 0) {
                    kinds--;
                }
                left++;
            }
            len = Math.max(len,right-left+1);
            right++;
        }
        return len;
    }

    public static void main(String[] args) {
        int[] arr = {3,3,3,1,2,1,1,2,3,3,4};
        int ret = totalFruit(arr);
        System.out.println(ret);
    }
}
